#ifndef PLAYER_H
#define PLAYER_H

#include <QString>
#include <QGraphicsSvgItem>
#include <QTextStream>

class player;

class card  {
    //Q_OBJECT
    friend bool cardsort(const card * a, const card * b);    
public:
    enum CSUIT { NOSUIT=-1, DIAMOND=0, CLUB=1, HEART, SPADE };
    enum CVALUE { NOVALUE=-1, TWO=0, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};

    card(int num, player * setowner=0);
    card(CSUIT s, CVALUE v, player * setowner = 0);

    CSUIT suit;
    CVALUE value;
    int index;
    QGraphicsSvgItem svg;
    player * owner;
    static int sv2num(const CSUIT & s, const CVALUE & v)
    {
        return s*13+v;
    }

    int sv2num() const;
    //card num2sv(int num);
    QString getSVGString();

    static QSvgRenderer *RENDERER;
    static QSizeF CARDSIZE;

    bool operator > (const card &); //USE for "who wins only"
    //friend bool card::operator >(const card &, const card &);
    bool operator < (const card &); //Use for sorting see qalgorithm
};




class player
{

public:
    player(const QString & n , bool human=true);
    player * partner;
    player * left;
    player * right;
    QGraphicsSimpleTextItem * txtBid;
    QGraphicsSimpleTextItem * txtScore;

    //helper functions to set up reverse poiters
    void setLeft(player *);
    void setRight(player *);
    void setPartner(player *);
    void setDealer();
    void setScoreboard(QGraphicsSimpleTextItem * b, QGraphicsSimpleTextItem * s);

    QString name;
    bool isHuman;
    bool isBiddingNil;
    int currentbid;
    int tricks;
    int score;
    bool hasHearts;
    bool hasClubs;
    bool hasDiamonds;
    bool hasSpades;

    QList<card *> hand;

    typedef int (player::*bid_fptr)();
    bid_fptr bid;
    int conservative_bid();

    typedef card * (player::*play_fptr)(card **,int,const QList<card *> &);
    play_fptr play;
    card * conservative_play(card * trick[],int trick_pos, const QList<card *> & discard);

    bool isDealer;
    int cards_in_suit(const card::CSUIT &, const QList<card *> &);
    int cards_in_suit(const card::CSUIT &);
    card::CVALUE value_in_suit(const card::CSUIT &, int position=0);
    bool spades_broken(const QList<card *> &);
    card * takecard(const card::CSUIT &, const int &);
    card * peakcard(const card::CSUIT &, const int &);
    bool iswinner(const card &, const QList<card *> &);
    card * findwinner(QList<card::CSUIT> suits, const QList<card *> & discard);
    card * current_winning_card(card **,int);
};


#endif // PLAYER_H
